<template>
	<view class="content">
		<view class="upload-box">
			<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" multiple :maxCount="1">
			</u-upload>
			<text class="name">点击上传头像</text>
		</view>
		<u--form labelPosition="left" :model="formData" ref="uForm" labelWidth="80">
			<u-form-item label="用户名" prop="userName" borderBottom>
				<u--input v-model="formData.userName" border="none" placeholder="请输入用户名"></u--input>
			</u-form-item>
			<u-form-item label="手机号" prop="phone" borderBottom>
				<u--input v-model="formData.phone" border="none" placeholder="请输入手机号"></u--input>
			</u-form-item>
			<u-form-item label="性别" prop="sex" borderBottom @click="showSex = true">
				<u--input v-model="formData.sex" disabled disabledColor="#ffffff" placeholder="请选择性别"
					border="none"></u--input>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>
			<u-button type="primary" shape="circle" text="保存" @click="submit" class="btn"></u-button>
		</u--form>
		<u-action-sheet :show="showSex" :actions="actions" title="请选择性别" :safeAreaInsetBottom="true"
			@close="showSex = false" @select="sexSelect">
		</u-action-sheet>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showSex: false,
				formData: {
					userName: '',
					phone: '',
					sex: '',
					sexValue: ''
				},
				actions: [{
						name: '男',
						id: 0
					},
					{
						name: '女',
						id: 1
					},
				],
				fileList: [],
				userData: uni.getStorageSync('user')
			}
		},
		onLoad() {
			if (uni.getStorageSync('user').sex) {
				this.formData.sex = this.actions[1].name
			} else {
				this.formData.sex = this.actions[0].name
			}
			this.formData.userName = uni.getStorageSync('user').name
			this.formData.phone = uni.getStorageSync('user').phone
			this.fileList.push({
				url: uni.getStorageSync('user').headImg
			})
		},
		methods: {
			submit() {
				let that = this
				// console.log(this.formData)
				that.$api.userAddOrUpdate({
					id: uni.getStorageSync('user').id,
					name: that.formData.userName,
					sex: that.formData.sexValue,
					phone: that.formData.phone,
					headImg: that.fileList[0].url
				}).then(res => {
					// console.log(res)
					that.$refs.uToast.show({
						type: 'success',
						message: '修改成功！',
						complete() {
							that.userData.headImg = that.fileList[0].url
							that.userData.sex = Boolean(that.formData.sexValue)
							that.userData.name = that.formData.userName
							that.userData.phone = that.formData.phone
							uni.setStorageSync('user', that.userData)
							uni.navigateBack()
						}
					})
				})
			},
			sexSelect(e) {
				this.formData.sex = e.name
				this.formData.sexValue = e.id
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			//上传图片
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://43.139.235.169:8081/cos/upload',
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							resolve(JSON.parse(res.data).resultObj)
						}
					});
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 40rpx;
		display: flex;
		flex-direction: column;

		.u-upload {
			align-items: center;
		}

		.upload-box {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-bottom: 40rpx;

			.name {
				margin-top: 20rpx;
				font-size: 30rpx;
			}
		}

		.u-form {
			display: flex;
			flex-direction: column;
		}

		.u-form-item {
			margin-bottom: 20rpx;
		}

		.btn {
			width: 80%;
			margin-top: 60rpx;
			background-color: #1678ff;
			border-color: #1678ff;
		}
	}
</style>